package com.itguigu.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itguigu.model.wechat.Menu;
import com.itguigu.vo.wechat.MenuVo;
import com.itguigu.wechat.mapper.WechatMenuMapper;
import com.itguigu.wechat.service.WechatMenuService;
import com.mysql.cj.xdevapi.JsonArray;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, Menu> implements WechatMenuService{

    @Resource
    private WxMpService wxMpServicel;

    /**
     * 获取公众号全部菜单
     * @return
     */
    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> resultList = new ArrayList<>();
        // 查询菜单所有数据
        List<Menu> menusList = baseMapper.selectList(null);
        // 过滤 得到 父节点数据  及 parentid=0的
        List<Menu> parentMenuList = menusList.stream()
                .filter(menu -> menu.getParentId().longValue() == 0)
                .toList();
        // 遍历每一个父节点
        for (Menu parentMenu : parentMenuList) {
            MenuVo parentMenuVo = new MenuVo();
            BeanUtils.copyProperties(parentMenu, parentMenuVo);

            // 过滤 父节点id = 当前父节点的id的 并排序  --->  找每个父菜单的子菜单
            List<Menu> childMenuList = menusList.stream()
                    .filter(menu -> menu.getParentId().longValue() == parentMenu.getId())
                    .sorted(Comparator.comparing(Menu::getSort))
                    .toList();
            List<MenuVo> childrenList = new ArrayList<>();
            // 遍历存储到 子目录集合
            for (Menu menu : childMenuList) {
                MenuVo childMenu = new MenuVo();
                BeanUtils.copyProperties(menu, childMenu);
                // 添加 孩子目录 到 孩子集合
                childrenList.add(childMenu);
            }
            // 添加 父 中的 孩子集合
            parentMenuVo.setChildren(childrenList);
            // 添加 父 到 结果
            resultList.add(parentMenuVo);
        }
        return resultList;
    }

    /**
     * 同步菜单功能
     * 从后台 --->  同步到 微信公众号
     * 封装成微信要求的 数据格式
     */
    @Override
    public void syncMenu() {
        // 获取 菜单数据
        List<MenuVo> menuVoList = this.findMenuInfo();
        // 按照微信数据要求格式进行封装 json数组
        JSONArray buttonList = new JSONArray();
        // 遍历层级结构菜单 -- 父菜单
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://oa.atguigu.cn/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = new JSONArray();
                for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                    JSONObject view = new JSONObject();
                    view.put("type", twoMenuVo.getType());
                    if(twoMenuVo.getType().equals("view")) {
                        view.put("name", twoMenuVo.getName());
                        //H5页面地址
                        view.put("url", "http://oa.atguigu.cn#"+twoMenuVo.getUrl());
                    } else {
                        view.put("name", twoMenuVo.getName());
                        view.put("key", twoMenuVo.getMeunKey());
                    }
                    subButton.add(view);
                }
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        // 调用工具完成推送
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            wxMpServicel.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除菜单功能
     */
    @Override
    public void removeMenu() {
        try {
            wxMpServicel.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}
